#!/bin/bash

##############################################################
#def vars

DBUSR='xx'
DBPWD='xxx'
DBSOCK='/opt/mysql/mysql.sock'
DBPORT=3306
TABFILE='/tab.lst'
MYCFGFILE='/etc/my.cnf'
BACKUPDIR="/xtrabackup/backup$(date '+%s')"

##############################################################
#generate table list

TABLIST=$(
/usr/bin/mysql -s -u${DBUSR} -p${DBPWD} --socket=${DBSOCK} --port=${DBPORT} \
<<EOF
set session group_concat_max_len=10*1024*1024;
select
    group_concat(concat(table_schema,'.',table_name)) tablist
from
    information_schema.tables t
where
NOT ((table_schema='db1' and table_name like 'tab1%') or
(table_schema='db2' and table_name like 'tab2%'));
EOF
)

##############################################################
#execute backup

mkdir -p ${BACKUPDIR}

/usr/local/bin/mydumper \
    --tables-list=${TABLIST} \
    --host=localhost \
    --socket=${DBSOCK} \
    --port=$DBPORT \
    --user=$DBUSR \
    --password=$DBPWD \
    --outputdir=${BACKUPDIR} \
    --events \
    --routines \
    --triggers \
    --rows=500000 \
    --build-empty-files \
    --threads=4 \
    --no-locks 

cp ${MYCFGFILE} ${BACKUPDIR}/my.cnf.$(date "+%s")

##############################################################
#delete old backup pieces

find /backup* -mtime +1 -name "*" -exec rm -rf {} \;